/* Copyright (c) 2008 Google Inc.
*  Copyright (c) 2009 Pierre Henri Kuate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Linq;
using org.opensocial.client;
using org.opensocial.data;

public static class DisplayAlbums
{
    public static void Run()
    {
        var c = new OpenSocialClient(Program.KnownGadgets["623061448914"]); // Orkut: http://opensocial-resources.googlecode.com/svn/samples/rest_rpc/sample.xml
        string ViewerId = "03067092798963641994"; // API DWH

        /*c = new OpenSocialClient(Program.KnownGadgets["http://opensocial-net-client.googlecode.com/svn/trunk/src/OpenSocial.Sample.Web/gadget.xml"]);
        ViewerId = "425505213"; // MySpace sample account/**/

        /*c = new OpenSocialClient(Program.KnownGadgets["hi5.com"]);
        ViewerId = "2"; // Hi5 random account/**/

        c.ViewerId = ViewerId;
        //c.RpcEndpoint = null; // RPC is preferred because it supports batch requests; so set it to null to use REST

        var me = c.fetchPerson();
        Output(me);

        var albums = c.fetchAlbums();
        if (albums.Count == 0)
            return;

        var newAlbum = c.createAlbum(new Album { Title = "Album Test " + System.DateTime.Now.ToString("u") });
        Output(newAlbum);

        var selectedAlbum = albums.OrderBy(a => a.MediaItemCount.Value).First();
        var newMediaItem = c.uploadMediaItem(selectedAlbum.Id,
            new MediaItem { Title = "Photo Test " + System.DateTime.Now.ToString("u") },
            System.IO.File.ReadAllBytes("photo.jpg"), "image/jpg", "image");
        Output(newMediaItem);

        foreach (var album in albums)
        {
            Output(album);
            //var mediaItems = c.fetchMediaItems("@me", "@self", album.Id);
            //Output(mediaItems);
            System.Console.Out.WriteLine();
        }

        System.Console.Out.WriteLine("--------------------");
        System.Console.Out.WriteLine("Press a key to exit!");
        System.Console.ReadKey();
    }

    private static void Output<T>(Response<T> entities)
        where T : OpenSocialObject
    {
        System.Console.Out.WriteLine("--------------------");

        System.Console.Out.WriteLine(entities.Count + " " + typeof(T).Name + "(s): startIndex=" + entities.startIndex + ", totalResults=" + entities.totalResults);

        foreach (var e in entities)
            System.Console.Out.WriteLine("- " + e);
    }

    private static void Output<T>(T entity)
    {
        System.Console.Out.WriteLine("--------------------");

        System.Console.Out.WriteLine(entity);
    }
}